// pages/01homePage/02openTable/openTable.js
var app = getApp()
var appData = getApp().globalData
Page({
    data: {
        cateBtnStateArr: [],
        goodsList: [],
        tableInfo: '',
        foodOrderList: [],
        foodNum: 0,
        totalPrice: 0,
    },

    onLoad: function (opt) {
        var res = wx.getSystemInfoSync()
        console.log('系统信息res', res)
        this.setData({
            win_h: res.windowHeight - 55
        })

        var obj = JSON.parse(opt.table);
        this.setData({
            tableInfo: obj
        })
        wx.setNavigationBarTitle({
            title: appData.shopInfo.shopInfo.shop + ' ' + obj.fenlei + obj.zhuo,
        })
        var c_list = appData.goodsCateList
        if (c_list) {
            this.setData({//页面用分类数据，如果全局里有就不请求
                goodsCateList: c_list
            })
            this.getGoodsData(0)
        } else {
            var that = this
            var param = { uid: appData.shopInfo.shopInfo.userid }
            getApp().getJsonData('api/space/product_cate.php', param, function (cateData) {
                that.handleCateData(cateData)
            })
        }
    },

    addFoodMenu: function (e) {
        var goodList = this.data.goodsList
        var goodsInfo = this.data.goodsList[e.currentTarget.id]
        var index = this.isExistAtOrder(goodsInfo)
        if (index == -1) {
            var food = {
                id: goodsInfo.id,
                name: goodsInfo.name,
                pic: goodsInfo.pic,
                price: goodsInfo.price,
                jjdangwei: goodsInfo.jjdangwei,
                num: 1
            }
            this.data.foodOrderList.push(food)
        } else {
            this.data.foodOrderList[index].num++
        }
        this.countNumAndPrice()
        this.goodsListAddNum()
        console.log('加点了菜this.data.foodOrderList', this.data.foodOrderList)
    },

    reduceNum: function (e) {
        //菜谱上减
        var i = e.currentTarget.id
        var list = this.data.goodsList
        list[i].num--
        this.setData({ goodsList: list })
        //订单里减
        this.reduceOrder(list[i].id)
    },

    reduceOrder: function (id) {//订单里减
        var list = this.data.foodOrderList
        for (var i = 0; i < list.length; i++) {
            if (id == list[i].id) {
                if (list[i].num > 1) {
                    list[i].num--
                } else {
                    list.splice(i, 1)
                }
                break
            }
        }
        this.setData({ foodOrderList: list })
        console.log('减后订单', list)
        this.countNumAndPrice()
    },

    getOrderFoodsIdArr: function (list) {
        var idArr = []
        for (var i = 0; i < list.length; i++) {
            idArr.push(list[i].id)
        }
        return idArr
    },

    goodsListAddNum: function () {//菜谱上已点的菜加上数量（-）

        var _list = this.data.foodOrderList
        var idArr = this.getOrderFoodsIdArr(_list)
        var list = this.data.goodsList

        for (var i = 0; i < list.length; i++) {
            var index = idArr.indexOf(list[i].id)
            if (index != -1) {
                list[i].num = _list[index].num
            }
        }
        console.log('新菜谱列表===', this.data.goodsList)
        this.setData({ goodsList: list })
    },

    countNumAndPrice: function () {
        var num = 0
        var price = 0
        var list = this.data.foodOrderList
        for (var i = 0; i < list.length; i++) {
            num += list[i].num
            price += (list[i].price * list[i].num)
        }
        this.setData({
            foodNum: num,
            totalPrice: price
        })
    },

    toFoodOrder: function () {
        if (this.data.foodOrderList.length == 0) {
            wx.showToast({
                title: '您还没点菜哦',
                image: '/pages/images/qzg_logo_90x.jpg'
            })
            return;
        }
        var tab = this.data.tableInfo
        var obj = {
            command: tab.orderid ? 'add' : 'eat',
            order: this.data.foodOrderList,
            zhuo: tab.id,
            orderid: tab.orderid,
            guestNum: tab.guestNum,
            fenlei:tab.fenlei,
            zhuohao:tab.zhuo
        }
        let order = JSON.stringify(obj)
        wx.navigateTo({
            url: '/pages/01homePage/03foodOrder/foodOrder?order=' + order,
        })
    },

    isExistAtOrder: function (goods) {
        var isExist = 0
        var order = this.data.foodOrderList
        for (var i = 0; i < order.length; i++) {
            if (goods.id == order[i].id) {
                isExist++
                return i
            }
        }
        return -1
    },

    addFoodMenuResult: function (resData) {
        console.log(resData)
        console.log('resData')
    },

    handleCateData: function (cateData) {
        var c_list = cateData.data
        if (cateData.code == 0) {
            this.setData({//页面用分类数据
                goodsCateList: c_list
            })
            appData.goodsCateList = c_list
            this.getGoodsData(0)
        }
    },

    cateClick: function (e) {
        var index = Number(e.currentTarget.id)
        // var cateId = this.data.goodsCateList[index].cateId
        this.getGoodsData(index)//获取下标为index的分类商品列表
        // this.renderingCateBtn(this.data.cateBtnStateArr, index)

    },

    getGoodsData: function (index) {
        this.renderingCateBtn(index)//渲染分类按钮
        console.log(this.data.goodsCateList[index])
        console.log('this.data.goodsCateList[index]')
        var that = this
        var param = {
            uid: appData.shopInfo.shopInfo.userid,
            cateid: this.data.goodsCateList[index].cateId
        }
        app.getJsonData('api/space/product_list.php', param, function (goodsData) {
            that.setData({ goodsList: goodsData.data })
            that.goodsListAddNum()
        })
    },
    //渲染分类按钮
    renderingCateBtn: function (index) {
        var btnStateArr = []
        var num = this.data.goodsCateList.length
        for (var i = 0; i < num; i++) {
            btnStateArr.push(false)
        }
        btnStateArr[index] = true
        this.setData({
            cateBtnStateArr: btnStateArr
        })
    },
})
